import json

from flask import request
from flask_cors import cross_origin

from app.controller.fund import *

'''
    @Author: webrainer
    @Date: 2023-5-25
    @Version: 0.1
    @Since: python3.11
    @See:
    @Description: 实现基金模块的基本功能，实现的功能有
        1. 基金的搜索(get_all,get_by_id,get_by_name),统一调用get_fund接口
        2. 基金的筛选(根据基金的类型，规模，风险等级，收益率等)，调用filter_fund接口
        3. 基金详细信息的获取，调用get_fund_detail接口
    
'''


@fund_module.route('/get_index', methods=['GET', 'POST'])
@cross_origin(supports_credentials=True)
def get_index():
    fund_service = Fund_Service()
    return [fund_service.get_index()]


@fund_module.route('/', methods=['GET', 'POST'])
@cross_origin(supports_credentials=True)
def get_all_funds():
    fund_service = Fund_Service()
    # 返回值如下类型 [{'code':'000001','name':'hello','netValue':1.0,'dailyGrowth':0.1}]
    # 将返回值变为如下类型
    # return [{'code':'000001','name':'hello','netValue':1.0,'dailyGrowth':0.1}]
    # return [json.dumps({'code':'000001','name':'基金','netValue':8.9,'dailyGrowth':0.57})]
    page = request.args.get('page')
    size = request.args.get('size')
    # #调用爬虫
    return fund_service.get_funds(page, size)


@fund_module.route('/get_one', methods=['GET', 'POST'])
@cross_origin(supports_credentials=True)
def get_one():
    symbol = request.args.get('symbol')
    if symbol is not None:
        return 'getting fund with symbol:' + symbol
    # 操作数据库
    return 'fund'


# 获取优选基金
@fund_module.route('/get_top/<nums>', methods=['GET', 'POST'])
def get_top():
    # 操作数据库
    nums = request.args.get('nums')
    return nums


# 获取基金详细信息

# 预测基金走势
@fund_module.route('/predict', methods=['GET', 'POST'])
def predict():
    # 操作数据库
    return 'fund'


@fund_module.route('/get_fund_detail', methods=['GET', 'POST'])
@cross_origin(supports_credentials=True)
def get_fund_detail():
    fund_service = Fund_Service()

    return fund_service.get_detail(request.args.get('code'))
